from django.db import models
from apps.common.account.models import Account
from apps.common.school.models import Faculty

# 审核状态类
from utils.common import BaseModel

CHECK_STATUS = (
    ('待审核', '待审核'),
    ('通过', '通过'),
    ('不通过', '不通过')
)

club_role = {
    1: '普通成员',
    2: '团支书',
    3: '会长',
    4: '老师',
}


# 社团分类表
class ClubType(BaseModel):
    name = models.CharField(verbose_name='分类名', max_length=10, unique=True)
    is_active = models.BooleanField(verbose_name='是否启用', default=True)

    class Meta:
        db_table = "club_type"
        verbose_name = "社团分类表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 指导单位表
# class BusinessUnit(BaseModel):


# 社团表
class Club(BaseModel):
    name = models.CharField(verbose_name='社团名', max_length=100)
    type = models.ForeignKey(ClubType, verbose_name='社团分类', related_name='type_club',
                             on_delete=models.CASCADE)
    established_time = models.DateField(verbose_name='成立时间', null=True)
    introduction = models.TextField(verbose_name="社团介绍")
    photo = models.JSONField(verbose_name='社团头像')
    image = models.JSONField(verbose_name='轮播图')
    file = models.JSONField(verbose_name='社团图片')
    first_check_status = models.CharField(verbose_name='老师审核状态', default='待审核',
                                          choices=CHECK_STATUS, max_length=20)
    second_check_status = models.CharField(verbose_name='团委审核状态', default='待审核',
                                           choices=CHECK_STATUS, max_length=20)
    apply_start_time = models.DateTimeField(verbose_name='报名开始时间', null=True, blank=True)
    apply_end_time = models.DateTimeField(verbose_name='报名结束时间', null=True, blank=True)
    business_unit = models.ForeignKey(Faculty, verbose_name='指导单位', related_name='faculty_club',
                                      null=True, blank=True, on_delete=models.CASCADE)

    is_active = models.BooleanField(verbose_name='是否启用', default=True)
    account = models.ForeignKey(Account, verbose_name='创始人', null=True, blank=True, on_delete=models.CASCADE)

    class Meta:
        db_table = "club"
        verbose_name = "社团表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 社团审核历史表
class ClubCheck(BaseModel):
    club = models.ForeignKey(Club, verbose_name='关联社团', related_name='club_check',
                             on_delete=models.CASCADE)
    account = models.ForeignKey(Account, verbose_name='审核人', related_name='account_club_check',
                                null=True, blank=True, on_delete=models.CASCADE)
    check_opinion = models.CharField(verbose_name='审核意见', max_length=50)
    check_status = models.CharField(verbose_name='审核状态', choices=CHECK_STATUS, max_length=20)
    check_level = models.IntegerField(verbose_name='审核级别', default=1)  # 1-一级审核， 2-二级审核

    class Meta:
        db_table = "club_check"
        verbose_name = "社团审核历史表"
        verbose_name_plural = verbose_name


# 社团成员表
class ClubMember(BaseModel):
    account = models.ForeignKey(Account, verbose_name='关联的账号', related_name='account_member',
                                on_delete=models.CASCADE)
    club = models.ForeignKey(Club, verbose_name='关联的社团', related_name='club_member',
                             on_delete=models.CASCADE)
    role = models.PositiveIntegerField(verbose_name='角色（数据字典中的club_role）', default=1)
    is_active = models.BooleanField(verbose_name='是否启用', default=True)

    class Meta:
        db_table = "club_member"
        verbose_name = "社团成员表"
        verbose_name_plural = verbose_name


# 学生申请加入社团表
class ClubApplication(BaseModel):
    club = models.ForeignKey(Club, verbose_name='关联的社团', related_name='club_application',
                             on_delete=models.CASCADE)
    account = models.ForeignKey(Account, verbose_name='关联的账号', related_name='account_application',
                                on_delete=models.CASCADE)
    reason = models.CharField(verbose_name='申请理由', max_length=100)
    file = models.JSONField(verbose_name='申请文件')
    is_active = models.BooleanField(verbose_name='是否启用', default=True)
    first_check_status = models.CharField(verbose_name='老师审核状态', default='待审核',
                                          choices=CHECK_STATUS, max_length=20)
    second_check_status = models.CharField(verbose_name='团委审核状态', default='待审核',
                                           choices=CHECK_STATUS, max_length=20)

    class Meta:
        db_table = "club_application"
        verbose_name = "申请社团表"
        verbose_name_plural = verbose_name


# 申请退团表
class ClubQuit(BaseModel):
    club = models.ForeignKey(Club, verbose_name='社团', related_name='club_quit',
                             null=True, blank=True, on_delete=models.CASCADE)
    account = models.ForeignKey(Account, verbose_name='申请人', related_name='account_club_quit',
                                on_delete=models.CASCADE)
    reason = models.TextField(verbose_name='退团理由', null=False)
    first_check_status = models.CharField(verbose_name='老师审核状态', default='待审核',
                                          choices=CHECK_STATUS, max_length=20)
    second_check_status = models.CharField(verbose_name='团委审核状态', default='待审核',
                                           choices=CHECK_STATUS, max_length=20)

    is_active = models.BooleanField(verbose_name='是否启用', default=True)

    class Meta:
        db_table = "club_quit"
        verbose_name = "申请退团表"
        verbose_name_plural = verbose_name


#  退团审核表
class ClubQuitCheck(BaseModel):
    club_quit = models.ForeignKey(ClubQuit, verbose_name='退团申请', related_name='club_quit_check',
                                  null=True, blank=True, on_delete=models.CASCADE)
    account = models.ForeignKey(Account, verbose_name='申请人', related_name='account_club_quit_check',
                                null=True, blank=True, on_delete=models.CASCADE)
    check_opinion = models.CharField(verbose_name='审核意见', default='', max_length=200)
    check_status = models.CharField(verbose_name='审核状态', default='待审核',
                                    choices=CHECK_STATUS, max_length=20)
    check_level = models.IntegerField(verbose_name='审核级别', default=1)

    class Meta:
        db_table = "club_quit_check"
        verbose_name = "退团审核表"
        verbose_name_plural = verbose_name


# 社团注销申请表
class ClubCancel(BaseModel):
    club = models.ForeignKey(Club, verbose_name='社团', related_name='club_cancel',
                             null=True, blank=True, on_delete=models.CASCADE)
    account = models.ForeignKey(Account, verbose_name='申请人', related_name='account_club_cancel',
                                null=True, blank=True, on_delete=models.CASCADE)
    record = models.TextField(verbose_name='记录', null=False)
    image = models.JSONField(verbose_name='会议图片')
    file = models.JSONField(verbose_name='相关资料')
    first_check_status = models.CharField(verbose_name='老师审核状态', default='待审核',
                                          choices=CHECK_STATUS, max_length=20)
    second_check_status = models.CharField(verbose_name='团委审核状态', default='待审核',
                                           choices=CHECK_STATUS, max_length=20)

    is_active = models.BooleanField(verbose_name='是否启用', default=True)

    class Meta:
        db_table = "club_cancel"
        verbose_name = "社团注销申请表"
        verbose_name_plural = verbose_name


# 社团注销审核表
class ClubCancelCheck(BaseModel):
    club_cancel = models.ForeignKey(ClubCancel, verbose_name='社团注销申请', related_name='club_cancel_check',
                                    null=True, blank=True, on_delete=models.CASCADE)
    account = models.ForeignKey(Account, verbose_name='审核人', related_name='account_club_cancel_check',
                                null=True, blank=True, on_delete=models.CASCADE)
    check_opinion = models.CharField(verbose_name='审核意见', default='', max_length=200)
    check_status = models.CharField(verbose_name='审核状态', default='待审核',
                                    choices=CHECK_STATUS, max_length=20)
    check_level = models.IntegerField(verbose_name='审核级别', default=1)

    class Meta:
        db_table = "club_cancel_check"
        verbose_name = "社团注销审核表"
        verbose_name_plural = verbose_name


class Log(models.Model):
    username = models.CharField(verbose_name='操作用户学工号', max_length=100, default='')
    name = models.CharField(verbose_name='操作用户姓名', max_length=100, default='')
    operate_type = models.SmallIntegerField(verbose_name='操作类型(1-新增 2-修改 3-删除 4-审核)')
    table_name = models.CharField(verbose_name='操作的数据库表', max_length=100)
    comment = models.TextField(verbose_name='操作信息')
    detail = models.TextField(verbose_name='详细信息', default='')
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        db_table = 'log'
        verbose_name_plural = verbose_name = '操作日志'